﻿using FRMS.DAL;
using FRMS_N71SP.Model;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;

namespace FRMS_N71SP.BUS
{
    public class N71SPDeviceBUS
    {
        public bool LoginDevice(string deviceip, string username, string password, int forcelogin)
        {
            try
            {
                N71SPLoginInfo n71SPLoginInfo = new N71SPLoginInfo();
                N71SPLoginInfoData n71SPLoginInfoData = new N71SPLoginInfoData();
                n71SPLoginInfoData.UserName = username;
                n71SPLoginInfoData.Password = password;
                n71SPLoginInfoData.ForceLogin = forcelogin;
                n71SPLoginInfo.ParamsInfo = n71SPLoginInfoData;

                string data = JsonConvert.SerializeObject(n71SPLoginInfo).Replace("ParamsInfo", "params");
                string re = N71SPHttpClientPost("http://" + deviceip + "/api/system/login", data);
                PersonReturnData PicLssueinfoReturn = JsonConvert.DeserializeObject<PersonReturnData>(re);

                if (PicLssueinfoReturn != null && PicLssueinfoReturn.result == 0 && PicLssueinfoReturn.method == "login")
                {
                    N71SPCallBackInfo n71SPCallBackInfo = new N71SPCallBackInfo();
                    n71SPCallBackInfo.RecordUrl = "";
                    n71SPCallBackInfo.HeartUrl = "";
                    n71SPCallBackInfo.HeartTime = 1;

                    string callback_data = JsonConvert.SerializeObject(n71SPCallBackInfo);
                    string callback_re = N71SPHttpClientPost("http://" + deviceip + "/api/system/login", callback_data);
                    PersonReturnData callback_Return = JsonConvert.DeserializeObject<PersonReturnData>(callback_re);
                    if (callback_Return != null && callback_Return.result == 0 && callback_Return.method == "setCallBack")
                    {
                        N71SPLoginReturn n71SPLoginReturn = JsonConvert.DeserializeObject<N71SPLoginReturn>(PicLssueinfoReturn.Params.ToString());
                        DeviceDAL deviceDAL = new DeviceDAL();
                        bool token_result = deviceDAL.UpdateToken(n71SPLoginReturn.Token, deviceip);
                    }
                }
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        public string N71SPHttpClientPost(string url, string datajson)
        {
            HttpClient httpClient = new HttpClient();//http对象
            //表头参数
            httpClient.DefaultRequestHeaders.Accept.Clear();
            httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

            //转为链接需要的格式
            var content = new StringContent(datajson, Encoding.UTF8, "application/json");
            //请求
            HttpResponseMessage response = httpClient.PostAsync(url, content).Result;
            if (response.IsSuccessStatusCode)
            {
                Task<string> t = response.Content.ReadAsStringAsync();
                if (t != null)
                {
                    return t.Result;
                }
            }
            return "";
        }
    }
}
